package com.huage_02.lesson_01;

import com.huage_02.util.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo02Pro {
    public static void main(String[] args) {

        List<Student> list = findAll();
        System.out.println(list);
    }
    public static List<Student> findAll(){
        List<Student> list = new ArrayList<Student>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {

            conn = JDBCUtils.getConnection();

            String sql = "select * from student_info";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            Student std = null;
            while (rs.next()){
                String name = rs.getString("name");
                String sex = rs.getString("sex");

                std = new Student();
                std.setName(name);
                std.setSex(sex);
                list.add(std);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeAll(conn, stmt, rs);
            return list;
        }
    }
}
